package com.paas.form.handler.util;

import com.paas.common.auth.service.IPermissionService;
import com.paas.common.util.AppUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.entity.FormModelParamsEntity;
import com.paas.form.entity.FormTaskParamsEntity;
import com.paas.params.model.common.Param;
import com.paas.params.param.ParamParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.text.StrSubstitutor;

import java.util.*;

@Slf4j
public class FormJobHandlerUtil {

    public static String formatSql(String excelSql, List<Map<String, Object>> params) {
        if (StringUtil.isNotEmpty(excelSql) && params != null && params.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < params.size(); i++) {
                Map<String, Object> paramMap = params.get(i);
                String tempType = paramMap.get("tempType").toString();
                Iterator<Map.Entry<String, Object>> iterator = paramMap.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> next = iterator.next();
                    if (next.getKey().equals("tempType")) {
                        continue;
                    } else {
                        if (tempType.toLowerCase().equals("int") || tempType.toLowerCase().equals("number")) {
                            map.put(next.getKey(), next.getValue());
                        } else {
                            map.put(next.getKey(), "'" + next.getValue() + "'");
                        }
                    }
                }
            }
            StrSubstitutor strSubstitutor = new StrSubstitutor(map);
            excelSql = strSubstitutor.replace(excelSql);
        }
        return excelSql;
    }


    /**
     * 解析参数
     */
    public static Map<String, Object> parseParams(List<FormTaskParamsEntity> formTaskParamsList, Map<String, FormModelParamsEntity> tempFormModelMap) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, String> columnMap = new HashMap<>();
        List<Param> params = new ArrayList<>();
        for (FormTaskParamsEntity taskParamsEntity : formTaskParamsList) {
            if (tempFormModelMap.containsKey(taskParamsEntity.getParamsId())) {
                FormModelParamsEntity modelParamsEntity = tempFormModelMap.get(taskParamsEntity.getParamsId());
                Param param = new Param();
                param.setCode(modelParamsEntity.getCode());
                param.setType(modelParamsEntity.getDataType().toUpperCase());
                param.setFunType(taskParamsEntity.getParamType());
                param.setText(taskParamsEntity.getExpression());
                columnMap.put(modelParamsEntity.getCode(), modelParamsEntity.getDataType().toLowerCase());
                params.add(param);
            }
        }
        resultMap.put("column", columnMap);
        resultMap.put("params", ParamParser.split(params));
        return resultMap;
    }

    public static String getToken(String appCode, String publicsKey) {
        IPermissionService permissionService = (IPermissionService) AppUtil.getBean(IPermissionService.class);
        log.info("mdm开始获取token");
        // 调用第三方应用获取令牌接口
        String token = permissionService.createThirdAppToken(appCode, publicsKey);
        log.info("mdm获取token成功：{}", token);
        return token;
    }
}
